package com.gregbugaj.gps.transform;

import java.util.List;

import com.gregbugaj.gps.Route;
import com.gregbugaj.gps.RoutePoint;

public class KMLTransformer implements Transformer {
	private boolean isTransformed;
	private StringBuilder builder;
	
	public KMLTransformer(){
		builder=new StringBuilder();
	}
	@Override	
	public void transform(Route route) {
		builder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");		
		builder.append("<kml xmlns=\"http://earth.google.com/kml/2.2\">");
		builder.append("<Document>");
		
		List<RoutePoint> points=route.getPoints();
		for(int i=0;i<points.size();i++){
			RoutePoint point=points.get(i);
			System.out.println(point);
			builder.append("<Placemark>");
			builder.append("<name>Test 2</name>");
			builder.append("<Point>");
			builder.append("<coordinates>");			
			builder.append(""+point.getLongitude().getDecimal());
			builder.append(",");
			builder.append(""+point.getLatitude().getDecimal());
			builder.append(",0");							
			builder.append("</coordinates>");
			builder.append("</Point>");
			builder.append("</Placemark>");
		}		
		builder.append("</Document>");		
		builder.append("</kml>");
		isTransformed=true;
	}

	@Override
	public String getTransformed() {
		if(!isTransformed){
			throw new IllegalStateException("Need to call transform(Route) before calling this method");
		}
		return builder.toString();
	}

}
